package com.oolatina.adapters;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.oolatina.R;

@SuppressLint("NewApi")
public class MusicAdapter extends BaseAdapter implements OnItemClickListener {
	private Context mContext;
	private LayoutInflater inflater;
	private ViewHolder holder;
	private String[] mLabels;
	private List<String> chosenStrings = new ArrayList<String>();
	private List<String> mData = Arrays.asList("Bachata", "Salsa", "Kizomba", "Cha-cha", "Merengue", "Rock'n Roll");
	Activity activity;
	
	public MusicAdapter(Context c, Activity a) {
		mContext = c;
		activity = a;

		try {
			inflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		} catch (Exception e) {
		}
	}

	public int getCount() {
		return mData.size();
	}

	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}

	static class ViewHolder {
		public TextView title;
		public ImageView stick;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) { // if it's not recycled, initialize some
									// attributes
			convertView = inflater.inflate(R.layout.popup_music_picker_item, null);
			holder = new ViewHolder();
			holder.title = (TextView) convertView
					.findViewById(R.id.musicItemTitle);
			holder.stick = (ImageView) convertView
					.findViewById(R.id.stickView);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.title.setText(mData.get(position));
		if (chosenStrings.contains(mData.get(position))) {
			holder.stick.setVisibility(View.VISIBLE);
		} else {
			holder.stick.setVisibility(View.INVISIBLE);
		}
		return convertView;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		String value = ((TextView)view.findViewById(R.id.musicItemTitle)).getText().toString();
		if (chosenStrings.contains(value)) {
			chosenStrings.remove(value);
			ImageView image = (ImageView)view.findViewById(R.id.stickView);
			image.setVisibility(View.INVISIBLE);
		} else {
			chosenStrings.add(value);
			ImageView image = (ImageView)view.findViewById(R.id.stickView);
			image.setVisibility(View.VISIBLE);
		}
	}

	public List<String> getChosenStrings() {
		return chosenStrings;
	}
	
}
